<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class sortController extends Controller {
   
    private $_page_number ;
    private $_sql;
    private $_sql2;
    private $_content;
    public function __construct($model, $action) {
        parent::__construct($model, $action);
       $this->_page_number = 4;
       $this->_sql ='';
       $this->_sql2 ='';
    }
    public function getSQL()
    { 
            $idCategory= $_POST['idCategory']; 
            $page = $_POST['page'];
            if($idCategory == 0)  {        
                 if(isset($_POST['sortby']) && isset($_POST['rank'])){       
                    $sortby = $_POST['sortby'];
                    $rank = $_POST['rank'];
                    if($sortby == '-1' || $sortby == '2') {
                                if($rank == '-1' || $rank == '2') {
                                       $this->_sql = 'select * from product  order by timeStart DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                                } else {
                                       $this->_sql = 'select * from product  order by timeStart ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                                }

                            }
                            
                    if($sortby == '1'){
                        if($rank == '-1' || $rank == '2') {
                               $this->_sql = 'select * from product  order by name DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        } else {
                               $this->_sql = 'select * from product  order by name ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        }
                    }

                    if($sortby == '3'){
                        if($rank == '-1' || $rank == '2')  {
                               $this->_sql = 'select * from product  order by timeEnd DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        } else {
                               $this->_sql = 'select * from product  order by timeEnd ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        }
                    }

                }  else {
                     $this->_sql = 'select * from product  Limit '.($page* $this->_page_number).','. $this->_page_number;
                }
                
                 $this->_sql2 = 'select * from product ';
                 
            } else {
                
                if(isset($_POST['sortby']) && isset($_POST['rank'])){
                    $sortby = $_POST['sortby'];
                    $rank = $_POST['rank'];
                    if($sortby == '-1' || $sortby == '2') {

                                if($rank == '-1' || $rank == '2') {
                                       $this->_sql = 'select * from product where idCategory='.$idCategory .' order by timeStart DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                                } else {
                                       $this->_sql = 'select * from product where idCategory='.$idCategory .' order by timeStart ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                                }
                            }

                    if($sortby == '1'){
                        if($rank == '-1' || $rank == '2')
                        {
                               $this->_sql = 'select * from product where idCategory='.$idCategory .' order by name DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        } else {
                               $this->_sql = 'select * from product where idCategory='.$idCategory .' order by name ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        }
                    }

                    if($sortby == '3'){
                        if($rank == '-1' || $rank == '2') {
                               $this->_sql = 'select * from product where idCategory='.$idCategory .' order by timeEnd DESC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        } else {
                              $this->_sql = 'select * from product where idCategory='.$idCategory .' order by timeEnd ASC Limit '.($page* $this->_page_number).','. $this->_page_number;
                        }
                    }
                }  else {
                     $this->_sql = 'select * from product where idCategory='.$idCategory .' Limit '.($page* $this->_page_number).','. $this->_page_number;
                }
                
               $this->_sql2 = 'select * from product where idCategory='.$idCategory ;
            }
        
    }
    public function getPagination()
    {
        $page = $_POST['page'];
        $count= $this->_productModel->getCount( $this->_sql2);
        $pageCounts=explode(".",$count/ $this->_page_number);
        $page_count = $pageCounts[0];

         $this->_content .='<div class="phantrang">';
         if($page!=0)
         {
              $this->_content .='<a href="javascript:sort(0,1)">Đầu |</a>  ';

         }
        for($i =0; $i< $page_count ; $i++)
        {
             $this->_content .='<a href="javascript:sort('.$i.')">    '.($i+1).' |</a>';
        }

         if($page <= $page_count-1 && isset($pageCounts[1]))
         {
              $this->_content .='  <a href="javascript:sort('.  $page_count.')">Cuối<a/>';

         }
    }
    public function getImageDetail($id) {
        return $this->_imageModel->getImageById($id);
    }
    public function index()
    {

        $this->_content='';
        $this->_content .= "<div class='title_new_product'>";
        $this->_content .= "SẢN PHẨM ĐƯỢC TÌM THẤY";
        $this->_content .= "</div>";
        $this->_content .= "<div class='list_new_product'>";
        
        if(isset($_POST))  {
            $this->getSQL();
            $this->_memcached->delete('sort'.md5( $this->_sql));
            if( $this->_memcached->get('sort'.md5( $this->_sql)))
            {
               $prod_result=$this->_memcached->get('sort'.  md5( $this->_sql));
               foreach ($prod_result as $item) {
               $this->_content .=' <a href="/product/detail/'.$item['idProduct'].'" class="link_product">
                                        <div class="body_hp">
                                            <img class="img_product" src="'.$this->getImageDetail($item['idProduct'])['link'].'" alt="sp" />
                                            <a href ="/product/detail/'.$item["idProduct"].'"> <p class="title_product">'.$item['name'].'</p> </a>
                                            <p class="date_end_product">Ngày kết thúc: '.$item['timeEnd'].'</p>
                                            <div class="clear_float"></div>
                                        </div>
                                    </a>';
               }
            } else {
                
               $prod_result=  $this->_productModel->getSearch( $this->_sql);
               //var_dump($this->_sql);die();
               foreach ($prod_result as $item) {
               $this->_content .=' <a href="/product/detail/'.$item['idProduct'].'" class="link_product">
                                        <div class="body_hp">
                                            <img class="img_product" src="'.$this->getImageDetail($item['idProduct'])['link'].'" alt="sp" />
                                            <a href ="/product/detail/'.$item["idProduct"].'"><p class="title_product">'.$item['name'].'</p></a>
                                            <p class="date_end_product">Ngày kết thúc: '.$item['timeEnd'].'</p>
                                            <div class="clear_float"></div>
                                        </div>
                                    </a>';
               }
               $this->_memcached->set('sort'.  md5( $this->_sql),$prod_result);               
            }
 
            $this->getPagination();
        }   
       
        $this->_content .='</div>';
        $this->_setView("index");
        $this->_view->set("content", $this->_content);
        return $this->_view->output();
       
    }       
}
?>